const mixin = require("../../mixins");
Component({
  behaviors: [mixin],
  data: {
    currIndex: 0,
    scrollLeft: 0,
    scrollTop: 0,
    isIDE: !1,
    duration: 0,
    scrollDistance: 0,
    transX: 0,
	utoolsShopLists:[]
  },
  properties: {
    mf_data: Object,
    utoolsData: Object
  },
  attached() {
    // 探索页 tab 跳转
    const DomPage = getCurrentPages()
    const tabName = DomPage[DomPage.length - 1].options.nvaName
    if (tabName && this.properties.utoolsData[0].title) {
      const titleData = this.properties.utoolsData[0].title.children
      const selectedIdx = titleData.findIndex(i => i.name1 == nvaName)
      this.setData({
        currIndex: Number(selectedIdx)
      })
    }
    const {
      duration: t = 0
    } = this.properties.mf_data;
    this.setData({
      isIDE: "devtools" == wx.getSystemInfoSync().platform,
      duration: t
    })
		if(this.properties.mf_data.autoPlay) {
			this.autoPlay()
		}

  },
  didUpdate(t, e) {},
  detached() {},
  async ready() {
    try {
      //  轮训查询当前轮播子集是否包含商品列表
      await this.filterUtoolsShopLists(this.data.mf_data, 'type', 'commodityList');

    } catch (e) {
    }
  },
  methods: {
    autoPlay() {
      let {
        autoPlay =false,
        autoPlayInterval=3000
      } = this.properties.mf_data;
      autoPlay && (this.timer = setInterval(() => {
        this.handleTouchEnd(-1)
      }, autoPlayInterval))
    },
    bindClick(t) {
      const {
        index: e
      } = t.currentTarget.dataset, {
        duration: a = 0,
        title: {
          children
        }
      } = this.properties.mf_data;

      this.switch || (this.timerTouch(a), this.setData({
        scrollLeft: this.translateDistance(Number(e)),
        currIndex: Number(e)
      }))
      // if(children[e]?.label1 == 'ProductBannerExpo')
      this.triggerEvent('utoolsEvents', {
        data: {
          type: 'rewriteTabCurrIndex',
          data: {
			name:  this.properties.mf_data.name1,
            currIndex: Number(e),
          }
        }
      }, {
        bubbles: true,
        composed: true
      });
      //  神策埋点-轮播嵌套线上精选模块
      if(this.data.utoolsShopLists && this.data.utoolsShopLists.length){
        const _tag = this.data.utoolsShopLists[Number(e)] || null;
         this.trackProductBannerExpo(_tag,  Number(e))
      }
      if (children[e]?.trackingParams) {
        this.triggerEvent('triggerComponent', {
          data: {
            trackData: children[e]?.trackingParams,
          }
        }, {
          bubbles: true,
          composed: true
        });
      }
    },
    translateDistance(t = 0) {
      let e, a = this.properties.mf_data.title,
        i = Number(a.children[0].style.width),
        s = i * a.children.length;
      e = i * (t + 1) >= 726 && i * (t + 1) <= s ? t <= a.children.length - 2 ? t - 1 : t - 2 : 0;
      let n = i * e > 0 ? i * e : 0;
      return Number(n)
    },
    touchStart(t) {
    	const {
    		clientX: e,
    		clientY: a
    	} = t.changedTouches[0];
    	this.startX = e
    	this.startY = a

    },
    touchMove() {},
    touchEnd(t) {
    	if (Date.now() - this.startTime < this.data.duration) return;
    	const {
    		clientX: e,
    		clientY: a
    	} = t.changedTouches[0];
    	let  _x = e - this.startX;
    	 // Math.abs(a) < 20 || (this.startTime = Date.now(), this.handleTouchEnd(a))
    	let y = Math.abs(this.startY - a)
    	let x = Math.abs(_x)

    	 if(x>(y+10) &&  x>20 && y<200) {
    		  (this.startTime = Date.now(), this.handleTouchEnd(_x))
    	 }

    },
    timerTouch(t = 200) {
      this.switch = !0, setTimeout(() => {
        this.switch = !1
      }, t)
    },
    handleTouchEnd(t) {
      const {
        animationType: e
      } = this.properties.mf_data;
      switch (e) {
        case "渐隐渐显":
          this.handleAnimationTypeByOpacity(t);
          break;
        case "滑动滚动":
          this.handleAnimationTypeByTranslate(t)
      }
    },
    handleAnimationTypeByOpacity(t) {
      let {
        currIndex: e
      } = this.data;
      const {
        children: a = []
      } = this.properties.mf_data;
      let i = Number(e);
      t > 0 ? 0 === i ? i = a.length - 1 : i -= 1 : i === a.length - 1 ? i = 0 : i += 1;
      const s = {
        currentTarget: {
          dataset: {
            index: i
          }
        }
      };
      this.bindClick(s)
    },
    handleAnimationTypeByTranslate(t) {
      let {
        currIndex: e
      } = this.data;
      const {
        children: a = []
      } = this.properties.mf_data;
      let i = Number(e);
      if (t > 0) {
        if (0 === i) return void this.setData({
          currIndex: i - 1,
          scrollLeft: this.translateDistance(a.length - 1)
        }, () => {
          setTimeout(() => {
            this.setData({
              currIndex: a.length - 1,
              duration: 0
            }, () => {
              setTimeout(() => {
                this.setData({
                  duration: this.properties
                    .mf_data.duration
                })
              }, 16.7)
            })
          }, this.data.duration)
        });
        i -= 1
      } else {
        if (i === a.length - 1) return void this.setData({
          currIndex: i + 1,
          scrollLeft: this.translateDistance(0)
        }, () => {
          setTimeout(() => {
            this.setData({
              currIndex: 0,
              duration: 0
            }, () => {
              setTimeout(() => {
                this.setData({
                  duration: this.properties
                    .mf_data.duration
                })
              }, 16.7)
            })
          }, this.data.duration)
        });
        i += 1
      }
      const s = {
        currentTarget: {
          dataset: {
            index: i
          }
        }
      };
      this.bindClick(s)
    },
	//  筛选utools json中的商品列表对象
	async filterUtoolsShopLists(data,key,value){
	  try{
	    if(data && data[key] && data[key] === value){
	      this.data.utoolsShopLists.push(data)
	      return true;
	    }
	    if(data && data.children && data.children.length){
	      for(let i=0;i<data.children.length;i++){
	        if(data.children[i]){
	          this.filterUtoolsShopLists(data.children[i], key,value);
	        }
	      }
	    }
	    return false;
	  }catch(e){}
	},
	//  线上精选神策埋点
	trackProductBannerExpo(datas){
	  try{
	    if(datas && datas.children && datas.trackingParams){
	      let _trackData = JSON.parse(datas.trackingParams);
	      if(_trackData.name && _trackData.data){
	        const page = getCurrentPages();
	        //  获取父级商品列表
	        let { utoolsClassGoodsLists } = page[page.length - 1].data;
	        let list = utoolsClassGoodsLists;
	        let product_sku_list = [];
	        let product_name_list = [];
	        datas.children.forEach(item=>{
	          let goods = list.filter(it => it.itemId == item.itemId)[0];
	          if(goods){
	            if(goods.skuCodes && goods.skuCodes[0]) product_sku_list.push(goods.skuCodes[0]);
	            if(goods.itemName) product_name_list.push(goods.itemName);
	          }
	        })
	        if(product_sku_list.length && product_name_list.length){
	          _trackData.data.product_sku_list = product_sku_list;
	          _trackData.data.product_name_list = product_name_list;
	          this.triggerEvent('triggerComponent', {
	            data: {
	              trackData: JSON.stringify(_trackData),
	              type: "track"
	            }
	          }, {
	            bubbles: true,
	            composed: true
	          });
	        }
	      }
	    }
	  }catch(e){}
	},
  }
});
